package com.elvende.web.client.view.life;

import java.util.HashMap;

import com.elvende.web.client.dto.Learning;
import com.elvende.web.client.presenter.life.LearningPresenter;
import com.elvende.web.client.service.life.LearningService;
import com.elvende.web.client.service.life.LearningServiceAsync;
import com.elvende.web.client.view.View;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class LearningView extends View implements LearningPresenter.Display{
	
	interface LearningUiBinder extends UiBinder<Widget, LearningView> {}
	private static LearningUiBinder uiBinder = GWT.create(LearningUiBinder.class);
	LearningServiceAsync rpcService = GWT.create(LearningService.class);
		
	public static Learning[] studyList;
	
	@UiField
	FlexTable header;	
	@UiField
	FlexTable table;
	@UiField
	FlexTable keepTable;		
	@UiField
	FlexTable removeTable;	
		
	FlexTable activeTable;
	
	final int ORIG_TABLE = 0;
	final int KEEP_TABLE = 1;
	final int REMOVE_TABLE = 2;
	
	@UiField
	Button study;	
	@UiField
	Button remove;
	@UiField
	Button unsort;
	@UiField
	Button goStudy;
	
	@UiField
	TabLayoutPanel tabPanel;
	
	@UiField
	Button selectAll;	
	@UiField
	Button selectNone;

	private HashMap<String, Learning> origMap = new HashMap<String, Learning>(); 
		
	public void MoveToRawList(){}
	public void MoveToStudyList(){}
	public void ShuffleStudyList(){}
	
	
	
	public LearningView() {
		initWidget(uiBinder.createAndBindUi(this));	
		initGui();
	}
	
	public HasClickHandlers getStudyButton(){
		return goStudy;
	}
	
	public void setData(Learning[] result) {
		for (int i=0; i< result.length; i++){
			CheckBox c = new CheckBox();
			c.setName(result[i].getKey());
			setTableData(table,i,c,result[i]);			
			origMap.put(result[i].getKey(), result[i]);
		}					
	}		

	public void setTableData(FlexTable table, int row, CheckBox c, String data){
		table.setWidget(row, 0, c);
		table.setText(row, 1, data);		
	}
	
	public void setTableData(FlexTable table, int row, CheckBox c, Learning data){
		table.setWidget(row, 0, c);
		table.setText(row, 1, data.getName());
	}	
	
	public FlexTable getActiveTable(){
		int i = tabPanel.getSelectedIndex();
		switch(i){
		case 	ORIG_TABLE : return table;
		case	KEEP_TABLE : return keepTable;
		case	REMOVE_TABLE : return removeTable;
		}
		return null;
	}
	
	 private void initGui(){	
		 tabPanel.setStyleName(".gwt-TabLayoutPanel");
		 tabPanel.setTabText(0, "Unsorted");
		 tabPanel.setTabText(1, "Study");
		 tabPanel.setTabText(2, "Remove");
		 
		 study.setText("Keep");
		 remove.setText("Remove");
		 unsort.setText("Unsort");
		 selectAll.setText("All");
		 selectNone.setText("None");		 
		 goStudy.setText("Go Study");
		 
		 controlWidget(study);
		 controlWidget(remove);
		 controlWidget(unsort);
		 controlWidget(selectAll);
		 controlWidget(selectNone);
		 controlWidget(goStudy);

		 header.getColumnFormatter().setWidth(0, "20px");
		 header.getColumnFormatter().setWidth(1, "192px");
		 
		 table.setBorderWidth(1);
		 table.getColumnFormatter().setWidth(0, "20px");		    
		 table.getColumnFormatter().setWidth(1, "192px");
		 
		 removeTable.setBorderWidth(1);
		 removeTable.getColumnFormatter().setWidth(0, "20px");		    
		 removeTable.getColumnFormatter().setWidth(1, "192px");		    

		 keepTable.setBorderWidth(1);
		 keepTable.getColumnFormatter().setWidth(0, "20px");		    
		 keepTable.getColumnFormatter().setWidth(1, "192px");		    
		 		 		 
		 tabPanel.addSelectionHandler(new SelectionHandler<Integer>(){
			public void onSelection(SelectionEvent<Integer> event) { 
					tabEvent(event.getSelectedItem());
			}
		 });
		 
		 tabPanel.selectTab(ORIG_TABLE);
		 tabEvent(ORIG_TABLE);
	 }
	 
	 private void tabEvent(int tabId){
			setUpdatable();
			switch(tabId){
				case 	ORIG_TABLE : 
						unsort.setEnabled(false);
						activeTable = table;
						break;
				case	KEEP_TABLE : 
						study.setEnabled(false);
						activeTable = keepTable;
						break;
				case	REMOVE_TABLE : 
						remove.setEnabled(false);
						activeTable = removeTable;							
			}		
	 }
	 
	 @UiHandler("study")
	  void onStudyClicked(ClickEvent event) {
		 for (int i=activeTable.getRowCount()-1; i>-1; i--){
			 CheckBox cb = (CheckBox) activeTable.getWidget(i,0);
			 if (cb.getValue()){
				 setTableData(keepTable, keepTable.getRowCount(), cb, activeTable.getText(i, 1));			 
				 activeTable.removeRow(i);
			 }			
		 }
	 }		 
	 
	 @UiHandler("remove")
	  void onRemoveClicked(ClickEvent event) {
		 for (int i=activeTable.getRowCount()-1; i>-1; i--){
			 CheckBox cb = (CheckBox) activeTable.getWidget(i,0);
			 if (cb.getValue()){
				 setTableData(removeTable, removeTable.getRowCount(), cb, activeTable.getText(i, 1));			 
				 activeTable.removeRow(i);
			 }			
		 }
	 }		
	 
	 @UiHandler("unsort")
	  void onUnsortClicked(ClickEvent event) {
		 for (int i=activeTable.getRowCount()-1; i>-1; i--){
			 CheckBox cb = (CheckBox) activeTable.getWidget(i,0);
			 if (cb.getValue()){
				 setTableData(table, table.getRowCount(), cb, activeTable.getText(i, 1));			 
				 activeTable.removeRow(i);
			 }			
		 }
	 }	 
	 
	 @UiHandler("selectAll")
	  void onSelectAllClicked(ClickEvent event) {
		 selectAll(true);
	 }
	 
	 @UiHandler("selectNone")
	  void onSelectNoneClicked(ClickEvent event) {
		 selectAll(false);
	 }
	 
	 @UiHandler("goStudy")
	  void onGoStudyClicked(ClickEvent event) {
		 studyList = new Learning[table.getRowCount()];
		 for (int i=table.getRowCount()-1; i>-1; i--){
			 CheckBox cb = (CheckBox) table.getWidget(i,0);
			 studyList[i] = origMap.get(cb.getName());
		 }		 
	 }
	 
	 void selectAll(boolean select){
		 FlexTable table = getActiveTable();
		 for (int i=table.getRowCount()-1; i>-1; i--){
			 CheckBox cb = (CheckBox) table.getWidget(i,0);
			 cb.setValue(select);
		 }
	 }
}
